function Guess-Number {
    # 生成随机数字
    $randomNumber = Get-Random -Minimum 1 -Maximum 101
    $attempts = 0
    $guessed = $false
    
    Write-Host "欢迎来到猜数字游戏！" -ForegroundColor Green
    Write-Host "我已经想好了一个1到100之间的数字，请开始猜测吧！" -ForegroundColor Yellow
    
    while (-not $guessed) {
        $attempts++
        $guess = Read-Host "请输入你的猜测（第 $attempts 次尝试）"
        
        # 验证输入是否为有效数字
        if (-not ($guess -match '^\d+$')) {
            Write-Host "请输入有效的数字！" -ForegroundColor Red
            continue
        }
        
        $guess = [int]$guess
        
        if ($guess -lt $randomNumber) {
            Write-Host "太低了！再试一次。" -ForegroundColor Blue
        }
        elseif ($guess -gt $randomNumber) {
            Write-Host "太高了！再试一次。" -ForegroundColor Magenta
        }
        else {
            $guessed = $true
            Write-Host "恭喜你！你猜对了！数字是 $randomNumber" -ForegroundColor Green
            Write-Host "你总共尝试了 $attempts 次" -ForegroundColor Cyan
        }
    }
}

# 启动游戏
Guess-Number